#include<bits/stdc++.h>
using namespace std;
string s1,s2;
int f[10];
map<string,int> q;
int main(){
	freopen("network.in","r",stdin);
	freopen("network.out","w",stdout);
	int n;
	cin>>n;
	for(int j=1;j<=n;j++){
		cin>>s1>>s2;
		s2+=".";
		int ans_1=0,ans_2=0,x=0,m=0;
		for(int i=0;i<s2.size();i++){
			if(s2[i]=='0'&&s2[i+1]<=57&&s2[i+1]>=48&&((s2[i-1]<48)||(s2[i-1]>57)))
			{
				cout<<"ERR"<<endl;
				cout<<1<<endl;
			}
			if(s2[i]==':')
			  ans_1++;
			if(s2[i]=='.')
			  ans_2++;
			if(s2[i]>=48&&s2[i]<=57){
				x=x*10+int(s2[i]-'0');
			}
			else
			{
				f[++m]=x;
				x=0;
			}
		}
		if(ans_1!=1)
		{
			cout<<"ERR"<<endl;
			continue;
		}
		if(ans_2!=4)
		{
			cout<<"ERR"<<endl;
			continue;
		}
		int a=f[1],b=f[2],c=f[3],d=f[4],e=f[5];
		if(a<=255&&b<=255&&c<=255&&d<=255&&e<=65535)
		{
			if(s1=="Server")
			{
				if(q[s2]==0)
				{
					q[s2]=j;
					cout<<"OK"<<endl;
				}
				else
				  cout<<"FAIL"<<endl;
			}
			else{
				if(q[s2]==0)
				{
					cout<<"FAIL"<<endl;
				}
				else
				  cout<<q[s2]<<endl;
			}
		}
		else
		  cout<<"ERR"<<endl;
	}
	return 0; 
}
